{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "034f7360",
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "import os\n",
    "import json\n",
    "from mcp import ClientSession, StdioServerParameters\n",
    "from mcp.client.stdio import stdio_client\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e71adfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test Filesystem MCP Server\n",
    "async def test_filesystem_server():\n",
    "    current_dir = os.getcwd()\n",
    "    \n",
    "    # Create test directory for filesystem operations\n",
    "    test_dir = os.path.join(current_dir, \"test-filesystem\")\n",
    "    os.makedirs(test_dir, exist_ok=True)\n",
    "    print(f\"📁 Test directory: {test_dir}\\n\")\n",
    "\n",
    "    os.makedirs(os.path.join(test_dir, \"reports\"), exist_ok=True)\n",
    "    \n",
    "    # Connect to Filesystem MCP server\n",
    "    # This server operates on the test-filesystem directory\n",
    "    server_params = StdioServerParameters(\n",
    "        command=\"npx\",\n",
    "        args=[\"-y\", \"@modelcontextprotocol/server-filesystem\", test_dir]\n",
    "    )\n",
    "    \n",
    "    async with stdio_client(server_params) as (read, write):\n",
    "        async with ClientSession(read, write) as session:\n",
    "            await session.initialize()\n",
    "            \n",
    "            # List available tools\n",
    "            print(\"📋 Available Filesystem Tools:\")\n",
    "            tools_response = await session.list_tools()\n",
    "            for tool in tools_response.tools:\n",
    "                print(f\"  - {tool.name}: {tool.description}\")\n",
    "            \n",
    "            print(\"\\n💾 Testing Filesystem Operations:\")\n",
    "            \n",
    "            # Test 1: Write a text file\n",
    "            print(\"\\n1. Writing text file...\")\n",
    "            result1 = await session.call_tool(\"write_file\", {\n",
    "                \"path\": \"test-filesystem/test.txt\",\n",
    "                \"content\": \"Hello from Filesystem MCP Server!\\nThis is a test file.\"\n",
    "            })\n",
    "            print(f\"   Result: {result1.content[0].text if result1.content else 'Success'}\")\n",
    "            \n",
    "            # Verify file exists\n",
    "            file_path = os.path.join(test_dir, \"test.txt\")\n",
    "            if os.path.exists(file_path):\n",
    "                print(f\"   ✅ File created: {file_path}\")\n",
    "            \n",
    "            # Test 2: Read the file back\n",
    "            print(\"\\n2. Reading text file...\")\n",
    "            result2 = await session.call_tool(\"read_file\", {\n",
    "                \"path\": \"test-filesystem/test.txt\"\n",
    "            })\n",
    "            print(f\"   Content: {result2.content[0].text[:100] if result2.content else 'No content'}\")\n",
    "            \n",
    "            # Test 3: Write a JSON file (like a test report)\n",
    "            print(\"\\n3. Writing JSON report...\")\n",
    "            test_report = {\n",
    "                \"feature\": \"Todo App Test\",\n",
    "                \"scenarios\": [\n",
    "                    {\n",
    "                        \"name\": \"Add todo item\",\n",
    "                        \"status\": \"PASSED\",\n",
    "                        \"steps\": 5\n",
    "                    },\n",
    "                    {\n",
    "                        \"name\": \"Mark complete\",\n",
    "                        \"status\": \"PASSED\",\n",
    "                        \"steps\": 3\n",
    "                    }\n",
    "                ],\n",
    "                \"summary\": {\n",
    "                    \"total\": 2,\n",
    "                    \"passed\": 2,\n",
    "                    \"failed\": 0\n",
    "                }\n",
    "            }\n",
    "            \n",
    "            result3 = await session.call_tool(\"write_file\", {\n",
    "                \"path\": \"test-filesystem/reports/test_report.json\",\n",
    "                \"content\": json.dumps(test_report, indent=2)\n",
    "            })\n",
    "            print(f\"   Result: {result3.content[0].text if result3.content else 'Success'}\")\n",
    "            \n",
    "            # Test 4: List directory contents\n",
    "            print(\"\\n4. Listing directory contents...\")\n",
    "            result4 = await session.call_tool(\"list_directory\", {\n",
    "                \"path\": \"test-filesystem\"\n",
    "            })\n",
    "            print(f\"   Files/Folders:\")\n",
    "            for item in result4.content[0].text.split('\\n') if result4.content else []:\n",
    "                if item.strip():\n",
    "                    print(f\"     - {item}\")\n",
    "            \n",
    "            # Test 5: Read the JSON report back\n",
    "            print(\"\\n5. Reading JSON report...\")\n",
    "            result5 = await session.call_tool(\"read_file\", {\n",
    "                \"path\": \"test-filesystem/reports/test_report.json\"\n",
    "            })\n",
    "            if result5.content:\n",
    "                report_data = json.loads(result5.content[0].text)\n",
    "                print(f\"   Report Feature: {report_data['feature']}\")\n",
    "                print(f\"   Total Scenarios: {report_data['summary']['total']}\")\n",
    "                print(f\"   Passed: {report_data['summary']['passed']}\")\n",
    "            \n",
    "            # Test 6: Create multiple files (like screenshots metadata)\n",
    "            os.makedirs(os.path.join(test_dir, \"screenshots\"), exist_ok=True)\n",
    "            print(\"\\n6. Writing multiple files...\")\n",
    "            for i in range(3):\n",
    "                result = await session.call_tool(\"write_file\", {\n",
    "                    \"path\": f\"test-filesystem/screenshots/screenshot_{i}.txt\",\n",
    "                    \"content\": f\"Screenshot {i} metadata\\nTimestamp: 2024-10-22\\nTest: scenario_{i}\"\n",
    "                })\n",
    "            print(f\"   ✅ Created 3 screenshot metadata files\")\n",
    "            \n",
    "            # Test 7: List screenshots directory\n",
    "            print(\"\\n7. Listing screenshots directory...\")\n",
    "            result7 = await session.call_tool(\"list_directory\", {\n",
    "                \"path\": \"test-filesystem/screenshots\"\n",
    "            })\n",
    "            print(f\"   Screenshots:\")\n",
    "            for item in result7.content[0].text.split('\\n') if result7.content else []:\n",
    "                if item.strip():\n",
    "                    print(f\"     - {item}\")\n",
    "            \n",
    "            # Test 8: Get directory info (if available)\n",
    "            print(\"\\n8. Getting directory info...\")\n",
    "            result8 = await session.call_tool(\"get_file_info\", {\n",
    "                \"path\": \"test-filesystem/test.txt\"\n",
    "            }) if \"get_file_info\" in [t.name for t in tools_response.tools] else None\n",
    "            \n",
    "            if result8:\n",
    "                print(f\"   File info: {result8.content[0].text if result8.content else 'No info'}\")\n",
    "            else:\n",
    "                print(f\"   (get_file_info not available)\")\n",
    "            \n",
    "            print(\"\\n✅ All Filesystem tests completed!\")\n",
    "            print(f\"\\n📂 Check test files in: {test_dir}\")\n",
    "            print(f\"\\nDirectory structure:\")\n",
    "            for root, dirs, files in os.walk(test_dir):\n",
    "                level = root.replace(test_dir, '').count(os.sep)\n",
    "                indent = ' ' * 2 * level\n",
    "                print(f\"{indent}{os.path.basename(root)}/\")\n",
    "                subindent = ' ' * 2 * (level + 1)\n",
    "                for file in files:\n",
    "                    print(f\"{subindent}{file}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b4d7be1",
   "metadata": {},
   "outputs": [],
   "source": [
    "await test_filesystem_server()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7c2f2ac",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
